import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import router from './components/router/index.js';
// import axios from 'axios';
import scroll from 'vue-seamless-scroll';
Vue.use(scroll)

Vue.use(ElementUI);
Vue.config.productionTip = false
Vue.prototype.WebConfig = {
  title: '小区车辆管理系统',
}

var vue = new Vue({
  router,
  render: h => h(App),
}).$mount('#app')
//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
      document.title = vue.WebConfig.title + ' - ' + to.meta.title;
  }

  let user = sessionStorage.getItem('TokenID');
  if (!user && to.path !== '/login' && to.path !== '/Register') {
      next({
          path: '/login',
          query: {
              redirect: to.fullPath
          }
      });
  } else {
      next();
  }
})
//监听session存的数据
Vue.prototype.resetSetItem = function (key, newVal) {
  if (key === 'userInfo') {
    // 创建一个StorageEvent事件
    var newStorageEvent = document.createEvent('StorageEvent');
    const storage = {
      setItem: function (k, val) {
        window.sessionStorage.setItem(k, val);
        // 初始化创建的事件
        newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
        // 派发对象
        window.dispatchEvent(newStorageEvent)
      }
    }
    return storage.setItem(key, newVal);
  }
}